<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;
use think\config;
use Qiniu\Auth as Auth;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;

class Like extends Controller
{

    // 查看收藏
    public function index()
    {
        $session = Session::get();


        $id = $session['hmy_user']['id'];

        // 一级分类
        $category = DB::name('category')->where('pid','0,')->paginate(4);


        // 查询收藏

        $like = Db::table(['like' =>'l','picture' =>'p','hmy_user' => 'u'])
                    ->where('l.pict_id = p.id && l.user_id = '.$id.' && u.id = '.$id)
                    ->field('p.id,p.title,u.name,p.src')
                    ->select();

        $user = Db::table('hmy_user')
                    ->where('id = '.$id)
                    ->find();
        // 关注数量
        $like_num = count($like);


        return view('index@like/like',[
            'session'   => $session,
            'category'  => $category,
            'like'      => $like,
            'user'      => $user,
            'like_num'  => $like_num
        ]);
    }


    // 加入收藏
    public function collection()
    {

        // 接收图片id
        $pid = input('id');

        $session = Session::get();

        // 判断是否登录
        if ($session == null) {
           
            $info['status'] = false;
            $info['msg'] = '您还没有登录,请先登录!';

            return json($info);
        }

        // 查询
        $user_id = $session['hmy_user']['id'];

        // 查询是否已被收藏
        $like = Db::table(['picture' =>'p','hmy_user' =>'u','like'=>'l'])
                    ->where('l.pict_id = p.id && '.$pid.' = p.id && l.user_id = '.$user_id)
                    ->find();

        if ($like != null) {

            // 删除
            $like_del = Db::table('like')->where('pict_id',$pid)->where('user_id',$user_id)->delete();


            if ($like_del) {
                             
                    $info['status'] = true;
                    $info['msg'] = '已取消关注!';

                    return json($info);
            }

        }

        // 插入数据
        $data = [
            'user_id' => $user_id,
            'pict_id' => $pid
        ];

        // 执行数据
        $res = DB::table('like')->insert($data);

        // 返回
        if ($res > 0) {
                       
            $info['status'] = true;
            $info['msg'] = '已收藏';

            return json($info);
        }else{
                       
            $info['status'] = false;
            $info['msg'] = '收藏失败';

            return json($info);
        }
    }





    // 取消收藏
    public function cancel($id)
    {

        // 获取我的ID
        $session = Session::get();

        $uid = $session['hmy_user']['id'];

        // var_dump($id);die;   

        // 删除数据
        $res = Db::table('like')->where('pict_id',$id)->where('user_id',$uid)->delete();

        if($res > 0){
                      
            $info['status'] = true;
            $info['msg'] = '已取消收藏';

            return json($info);
        }else{
                      
            $info['status'] = false;
            $info['msg'] = '操作失败!';

            return json($info);
        }
    }



}
